package com.example.taobaounion.base;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;

import com.example.taobaounion.R;
import com.example.taobaounion.utils.LogUtils;

import butterknife.ButterKnife;
import butterknife.Unbinder;

public abstract class BaseFragment extends Fragment {

    private static final String TAG = "BaseFragment";
    protected View mSuccessView;
    private View mLoadingView;
    private View mErrorView;
    private View mEmptyView;
    private Unbinder mBind;

    public enum STATE {
        NONE,
        LOADING,
        SUCCESS,
        ERROR,
        EMPTY
    }


    //    @BindView(R.id.shift)
    protected FrameLayout mBaseContainer;

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {

        LogUtils.d(TAG, "base_fragment_layout -->" + R.layout.base_fragment_layout);
        LogUtils.d(TAG, "base_home_fragment_layout -->" + R.layout.base_home_fragment_layout);

        View rootView = loadRootView(inflater, container);


        LogUtils.d(TAG, "rootView -->" + rootView);

        mBaseContainer = rootView.findViewById(R.id.base_container);


        LogUtils.d(TAG, "mBaseContainer -->" + mBaseContainer);

        loadStateView(inflater, container);

        mBind = ButterKnife.bind(this, rootView);

        initVariable();


        initPresenter();
        initAdapter();
        initListener();
        initData();
        initView(rootView);


        return rootView;
    }

    protected void initVariable() {

    }


    protected void initAdapter() {

    }

    protected void initListener() {

    }

    protected View loadRootView(LayoutInflater inflater, ViewGroup container) {
        return inflater.inflate(R.layout.base_fragment_layout, container, false);
    }

    /**
     * 加载各种状态的view
     *
     * @param container 容器
     */
    private void loadStateView(LayoutInflater inflater, ViewGroup container) {

        mSuccessView = loadSuccessView(inflater, container);
        mBaseContainer.addView(mSuccessView);

        mLoadingView = loadLoadingView(inflater, container);
        mBaseContainer.addView(mLoadingView);


        mErrorView = loadErrorView(inflater, container);
        mBaseContainer.addView(mErrorView);

        mEmptyView = loadEmptyView(inflater, container);
        mBaseContainer.addView(mEmptyView);

        setUpState(STATE.NONE);
    }

    private View loadEmptyView(LayoutInflater inflater, ViewGroup container) {
        return inflater.inflate(R.layout.fragment_empty, container, false);
    }

    private View loadErrorView(LayoutInflater inflater, ViewGroup container) {
        View view = inflater.inflate(R.layout.fragment_error, container, false);

        view.findViewById(R.id.network_error_tips).setOnClickListener(v -> onRetryClick());

        return view;
    }

    protected void onRetryClick() {
        LogUtils.d(TAG, "onRetryClick...");
    }

    protected void setUpState(STATE state) {

        mLoadingView.setVisibility(View.GONE);
        mSuccessView.setVisibility(View.GONE);
        mErrorView.setVisibility(View.GONE);
        mEmptyView.setVisibility(View.GONE);

        switch (state) {

            case NONE:
                break;
            case LOADING:
                mLoadingView.setVisibility(View.VISIBLE);
                break;
            case SUCCESS:
                mSuccessView.setVisibility(View.VISIBLE);
                break;
            case ERROR:
                mErrorView.setVisibility(View.VISIBLE);
                break;
            case EMPTY:
                mEmptyView.setVisibility(View.VISIBLE);
                break;
            default:
                throw new IllegalStateException("Unexpected value: " + state);
        }

    }

    private View loadLoadingView(LayoutInflater inflater, ViewGroup container) {
        return inflater.inflate(R.layout.fragment_loading, container, false);
    }

    protected abstract void initView(View view);

    protected abstract void initPresenter();

    protected abstract void initData();

    private View loadSuccessView(LayoutInflater inflater, ViewGroup container) {

        int resId = getRootViewResId();
        return inflater.inflate(resId, container, false);
    }

    protected abstract int getRootViewResId();

    @Override
    public void onDestroy() {
        super.onDestroy();

        if (mBind != null) {
            mBind.unbind();
        }

        release();
    }

    protected abstract void release();
}
